package com.gt.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import net.sf.jxls.transformer.XLSTransformer;

public class XLSTransformerExportUtil {
	
	/**
	 * 根据模板生成Excel文件
	 * @param srcFilePath  模板路径
	 * @param destFilePath 导出文件路径
	 * @param beanParams   导出文件数据
	 */
    public static void createExcel(String srcFilePath, String destFilePath,Map<String,Object> beanParam){  
        //创建XLSTransformer对象  
        XLSTransformer transformer = new XLSTransformer();  
        try {  
            //生成Excel文件  
            transformer.transformXLS(srcFilePath, beanParam, destFilePath);
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
    }
    
    /**
     * 根据模板生成HSSFWorkbook 
     * @param srcFilePath  模板路径
     * @param beanParam    导出文件数据
     * @return  HSSFWorkbook
     */
    public static Workbook createExcel(String srcFilePath,Map<String,Object> beanParam){  
        //创建XLSTransformer对象  
        XLSTransformer transformer = new XLSTransformer();
        InputStream is = null;
        try {  
            //生成Excel文件  
        	File file = new File(srcFilePath);
        	if(file.exists()){
        		is = new FileInputStream(file);
        		return transformer.transformXLS(is, beanParam);
        	}
        } catch (Exception e) {  
        	System.out.println("-----------");
            e.printStackTrace();  
        } finally {
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
        return null;
    }
    
    //解析浏览器
  	public static boolean isIE(HttpServletRequest request){ 
  		  return request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 ? true : false; 
  	}
    
    public static void main(String[] args) {
		try {
			List<String[]> list2 = new ArrayList<String[]>();
			list2.add(new String[]{"1","北京","1234","2345","3456","4567"});
			list2.add(new String[]{"1","上海","2234","3345","4456","5567"});
			list2.add(new String[]{"1","天津","3234","4345","5456","6567"});
			
			List<Map<String, Object>> list = new ArrayList<>();
			Map<String,Object> map = new HashMap<>();
			map.put("city", "北a京");
			map.put("rcont", 1234);
			map.put("rsucont", 2222);
			map.put("scont", "12345");
			map.put("ssucont", "3333");
			list.add(map);
			String srcFilePath = "C:\\Users\\Administrator\\Desktop\\template_list.xls";  
			String destFilePath = "C:\\Users\\Administrator\\Desktop\\template_list_exprot.xls";
		    Map<String,Object> beanParams = new HashMap<String,Object>();  
	        beanParams.put("list", list); 
	        beanParams.put("list2", list2);  
	        beanParams.put("title", "测试导出");
			createExcel(srcFilePath, destFilePath, beanParams);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
